package com.tianshuo.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.WebApplicationInitializer;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.HandlesTypes;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * 初始化spring及springmvc
 * @author tianshuo
 */
@HandlesTypes(WebInit.class)
public class MyInitializer implements ServletContainerInitializer {
    Logger logger = LoggerFactory.getLogger(MyInitializer.class);
    @Override
    public void onStartup(Set<Class<?>> c, ServletContext ctx) throws ServletException {
        List<WebInit> list = new ArrayList<>();
        for (Class<?> aClass : c) {
            try {
                logger.info("{}",aClass);
                if(WebInit.class.isAssignableFrom(aClass)){
                    list.add((WebInit) aClass.newInstance());
                }
            } catch (InstantiationException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }

        for (WebInit webInit : list) {
            webInit.onStartup(ctx);
        }
    }
}
